package com.tec.security.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.tec.security.utils.file.FileResult;
import com.tec.security.utils.file.FileUploadService;
import com.tec.security.utils.security.entity.UserInfo;
import com.tec.security.utils.security.service.UserInformationService;
import com.tec.security.utils.security.service.UserKeyService;
import com.tec.security.utils.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "账号相关")
@RequestMapping("/user/")
public class UserController {
    /**
     * @description 自动注入UserServiceKey
     */
    private final UserKeyService userKeyService;
    private final FileUploadService fileUploadService;
    private final UserInformationService userInformationService;
    @Autowired
    public UserController(UserKeyService userKeyService, FileUploadService fileUploadService, UserInformationService userInformationService) {
        this.userKeyService = userKeyService;
        this.fileUploadService = fileUploadService;
        this.userInformationService = userInformationService;
    }

    /**
     * @description 登录
     * @author 陆林
     * @date 2023-11-21
     */
    @ApiOperation(value = "登录")
    @PostMapping("doLogin")
    public Result doLogin(String username, String password,boolean remember) {
        return userKeyService.login(username, password,remember);
    }

    /**
     * @description 查询登录状态
     * @author 陆林
     * @date 2023-11-21
     */
    @ApiOperation(value = "登录状态")
    @PostMapping ("isLogin")
    public Result isLogin() {
        return userKeyService.isLogin();
    }

    /**
     * @description 密码更新
     * @author 陆林
     * @date 2023-11-21
     */
    @ApiOperation(value = "修改密码")
    @PostMapping ("doPasswordUpdate")
    public Result doPasswordUpdate(String password,String update) {
        String key=StpUtil.getLoginId().toString();
        return userKeyService.doUpdatePassword(key,password,update);
    }

    /**
     * @description 邮件重设密码
     * @author 陆林
     * @date 2023-11-28
     */
    @ApiOperation(value = "找回密码")
    @PostMapping ("foundPassword")
    public Result foundPassword(String email,String password,String code) {
        return userKeyService.doPasswordReset(password,email,code);
    }

    /**
     * @description 注册
     * @author 陆林
     * @date 2023-11-21
     */
    @ApiOperation(value = "注册")
    @PostMapping ("signup")
    public Result signup(UserInfo user) {
        return userKeyService.signup(user);
    }

    /**
     * @description 获取详细信息
     * @author 陆林
     * @date 2023-11-21
     */
    @ApiOperation(value = "获取详细信息")
    @PostMapping ("getUserInfo")
    public Result getInfo() {
        return userKeyService.getUserInfo();
    }

    /**
     * @description 退出登录
     * @author 陆林
     * @date 2023-11-21
     */
    @ApiOperation(value = "退出登录")
    @PostMapping ("doLogout")
    public Result doLogout(HttpServletRequest request) {
        return userKeyService.doLogout("token");
    }

    /**
     * @description 头像上传
     * @author 陆林
     * @date 2023-11-30
     */
    @ApiOperation(value = "头像上传")
    @PostMapping("/avatarUrl")
    public FileResult imagesUpload(MultipartFile files, HttpServletRequest request){
       String key= (String) StpUtil.getLoginId();
       FileResult result=fileUploadService.fileUpload(files,request);
       userInformationService.UpdateURl(result.getData().get("url").toString(),key);
       return result;
    }
}
